package com.ruoyi.web.controller.chargepile;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.chargepile.domain.ChargePile;
import com.ruoyi.chargepile.domain.RepairRecord;
import com.ruoyi.chargepile.domain.dto.RepairRecordDto;
import com.ruoyi.chargepile.domain.dto.RepairRecordUpdateDto;
import com.ruoyi.chargepile.domain.enums.ChargePileStatusEnum;
import com.ruoyi.chargepile.domain.enums.RequestStatusEnum;
import com.ruoyi.chargepile.domain.enums.RolesEnum;
import com.ruoyi.chargepile.domain.vo.ChargeRecordsVo;
import com.ruoyi.chargepile.domain.vo.RepairRecordVo;
import com.ruoyi.chargepile.service.ChargePileService;
import com.ruoyi.chargepile.service.RepairRecordService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.util.DateRange;
import com.ruoyi.util.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 报修记录表 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/repair-record")
public class RepairRecordController {

    @Autowired
    private RepairRecordService repairRecordService;

    @Autowired
    private ChargePileService chargePileService;

    @Autowired
    private ISysRoleService iSysRoleService;

    @PostMapping("/addRepairRecord")
    public AjaxResult addRepairRecord(@RequestBody RepairRecord repairRecord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        repairRecord.setUserId(Math.toIntExact(user.getUserId()));
        repairRecord.setUserPhone(user.getPhonenumber());
        repairRecord.setRequestTime(new Date());

        ChargePile chargePile = chargePileService.getById(repairRecord.getChargePileId());
        chargePile.setStatus(ChargePileStatusEnum.IN_MAINTENANCE.getCode());

        if(repairRecordService.save(repairRecord) && chargePileService.updateById(chargePile)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @PostMapping("/updateRepairRecord")
    public AjaxResult updateRepairRecord(@RequestBody RepairRecordUpdateDto repairRecordUpdateDto) {
        RepairRecord repairRecord = repairRecordService.getById(repairRecordUpdateDto.getRepairRecordId());
        repairRecord.setAdminResponse(repairRecordUpdateDto.getAdminResponse());
        repairRecord.setRequestStatus(repairRecordUpdateDto.getRequestStatus());

        if(RequestStatusEnum.REPAIR_COMPLETED.getCode().equals(repairRecordUpdateDto.getRequestStatus())){
            ChargePile chargePile = chargePileService.getById(repairRecordUpdateDto.getChargePileId());
            chargePile.setStatus(ChargePileStatusEnum.IDLE.getCode());
            repairRecord.setCompletedTime(new Date());
            if(!chargePileService.updateById(chargePile)){
                return AjaxResult.error();
            }
        }

        if(repairRecordService.updateById(repairRecord)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @GetMapping("/listRepairRecord")
    public AjaxResult listRepairRecord(Pages pages, DateRange dateRange, RepairRecordDto repairRecordDto, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate beginTimeCompleted, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTimeCompleted) {
        PageHelper.startPage(pages.getPageNum(), pages.getPageSize());
        int pageNum = pages.getPageNum();
        int num = (pages.getPageNum() - 1) * pages.getPageSize();
        int size = pages.getPageNum() * pages.getPageSize();
        pages.setPageNum(num);
        pages.setPageSize(size);

        DateRange dateRangeCompleted = new DateRange();
        dateRangeCompleted.setBeginTime(beginTimeCompleted);
        dateRangeCompleted.setEndTime(endTimeCompleted);

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        List<SysRole> sysRoles = iSysRoleService.selectRolesByUserId(sysUser.getUserId());
        List<String> collectRoleKey = sysRoles.stream()
                .map(item -> item.getRoleKey())
                .collect(Collectors.toList());
        if (!collectRoleKey.contains(RolesEnum.ADMIN.getCode()) && !collectRoleKey.contains(RolesEnum.ADMINISTRATOR.getCode())){
            repairRecordDto.setUserId(Math.toIntExact(sysUser.getUserId()));
        }

        List<RepairRecordVo> sizeList = repairRecordService.listRepairRecord(null, dateRange, dateRangeCompleted, repairRecordDto);
        List<RepairRecordVo> list = repairRecordService.listRepairRecord(pages, dateRange, dateRangeCompleted, repairRecordDto);
        PageInfo<RepairRecordVo> chargeRecordsVoPageInfo = new PageInfo<>(list);
        chargeRecordsVoPageInfo.setTotal(sizeList.size());
        chargeRecordsVoPageInfo.setPages(pageNum);
        return AjaxResult.success(chargeRecordsVoPageInfo);
    }

}

